//
//  UserPaySettingController.m
//  HuLaQuan
//
//  Created by liyan on 16/1/22.
//  Copyright © 2016年 yuwubao. All rights reserved.
//

#import "UserPaySettingController.h"

#import "UserCenterNewCell.h"
#import "UserPaySettingsLimitCell.h"

#import "UserSwitchSettingCell.h"

#import "FundsLimitController.h"


#import "UserPaySettingsEngine.h"

@interface UserPaySettingController ()<PayLimitDelegate>


@property (nonatomic, strong) UserPaySettingsEngine *userPaySettingsEngine;


@property (nonatomic) BOOL isChangedLimitSettings;
@end

static NSString * const UserPaySettingsLimitCellIdentifier = @"UserPaySettingsLimitCellIdentifier";

@implementation UserPaySettingController

- (UserPaySettingsEngine *)userPaySettingsEngine
{
    if (!_userPaySettingsEngine) {
        _userPaySettingsEngine = [[UserPaySettingsEngine alloc] init];
    }
    return _userPaySettingsEngine;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}
-(void)setupUI
{
    self.title = @"支付设置";
    [self setupNavBackButton];
    
    [self.tableView registerNib:[UserPaySettingsLimitCell nib] forCellReuseIdentifier:@"UserPaySettingsLimitCellIdentifier"];
    [self.tableView registerNib:[UINib nibWithNibName:@"UserSwitchSettingCell" bundle:nil] forCellReuseIdentifier:@"switch"];
}

#pragma mark - tableViewDelegate

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if(section == 0)
    {
        if(self.selectedLimit && ![self.selectedLimit isEqualToString:@"0"])
        {
           return 2;
        }else{
            return 1;
        }
        
    }
    else{
        return 1;
    }
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    return  kNormalCellHeight;
    
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    if(self.selectedLimit && ![self.selectedLimit isEqualToString:@"0"])
    {
        return 2;
    }else{
        return 1;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if(section == 1)
    {
        return 40;
    }else{
        return 0.000001;
    }
    
    
    
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section

{
    if(section == 1){
        CGRect frameRect = CGRectMake(25, 0, 400, 40);
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 300, 40)];
        YuUserCellLabel *textSection = [[YuUserCellLabel alloc] initWithFrame:frameRect];
        textSection.text =[NSString stringWithFormat: @"使用手机付款，金额小于等于%@元／笔，无需输入支付密码",self.selectedLimit ? self.selectedLimit : @"200"];
        [view addSubview:textSection];
        return view;
    }else{
        return nil;
    }
    
    
    
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    switch (indexPath.section) {
        case 0:
        {
            if (indexPath.row == 0) {
                UserSwitchSettingCell *cell = [tableView dequeueReusableCellWithIdentifier:@"switch"];
                cell.titleLabel.text = @"小额免密支付";
                
                if (self.selectedLimit && ![self.selectedLimit isEqualToString:@"0"]){
                    [cell.swithButton setOn:YES];
                }else{
                    [cell.swithButton setOn:NO];
                }
                [cell.swithButton addTarget:self action:@selector(switchAction:) forControlEvents:UIControlEventValueChanged];
                
                return cell;
            }else{
                UserPaySettingsLimitCell *cell = [tableView dequeueReusableCellWithIdentifier:UserPaySettingsLimitCellIdentifier];
                cell.titleLabel.text = @"免密额度";
                

                if (self.selectedLimit) {
                    
                    cell.limitLabel.text = self.selectedLimit;
                }
                
                return cell;
            }
            
        }
            break;
        case 1:
        {
            UserSwitchSettingCell *cell = [tableView dequeueReusableCellWithIdentifier:@"switch"];
            cell.titleLabel.text = @"付款方式";
            return cell;

        }
            break;
        
        default:
        {
            return nil;
            
        }
            break;
    }
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (1 == indexPath.row) {
        FundsLimitController *funds = [[FundsLimitController alloc] init];
//        [funds returnText:^(NSString *showText) {
//            self.selectedLimit = showText;
//             //NSLog(@"~~~~~~!%@",_selectedLimit);
//            UILabel *label = (UILabel *)[self.view viewWithTag:101];
//            label.text = nil;
//            label.text = self.selectedLimit;
//            //[self.tableView reloadData];
//        }];
        funds.selectedLimit = self.selectedLimit;
        funds.delegate = self;
        [self.navigationController pushViewController:funds animated:YES];
    }
}
- (void)switchAction:(UISwitch *)sender {
    // NSLog(@"%@",)
    if(sender.isOn == YES)
    {
        self.selectedLimit = @"200";
    }else{
        self.selectedLimit = @"0";
    }
    
    [self updateServerPayLimit];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Call Back
- (void)setPaymentLimitFinish:(NSNotification*) notification
{
    
    [YuNetworkStatusUtilities dismissHUD];
    
    NSDictionary *messageDic = [notification object];//获取到传递的对象
    if ([messageDic[@"isSuccess"] boolValue]) {
        
        [self.view makeToast:@"设置小额免密成功"];
        
        [self.tableView reloadData];
    }else{
        [self showNetWorkError:messageDic[@"message"]];
        
    }
}
#pragma mark - Life Cycle
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:YES];
    
    
    NSLog(@"%@",self.userInfo);
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(setPaymentLimitFinish:) name:@"SetPaymentLimitFinish" object:nil];
    
    if (self.isFromUserCenter) {
        if (self.userInfo[@"payLimit"]) {
            self.selectedLimit = self.userInfo[@"payLimit"];
        }
    }
    
    if(self.isChangedLimitSettings){
        [self updateServerPayLimit];
    }
    
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:YES];
    
    
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"SetPaymentLimitFinish" object:nil];
    
    self.isFromUserCenter = NO;
    
}
- (void)updateServerPayLimit
{
    [YuNetworkStatusUtilities showProgressMessage:NETWORK_LOADING];
    [self.userPaySettingsEngine setPayLimit:self.selectedLimit];
}

#pragma mark -  Delegate

- (void)changePayLimit:(BOOL)isChanged
{
    self.isChangedLimitSettings = isChanged;
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
